package com.trtan.hr.service;

import com.trtan.hr.dao.HrRoleDao;
import com.trtan.hr.dao.MenuRoleDao;
import com.trtan.hr.dao.RoleDao;
import com.trtan.hr.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: trtan
 * @create: 2021-03-07 16:10
 * @description:
 **/
@Service
public class RoleService {

    @Autowired
    RoleDao roleDao;

    @Autowired
    MenuRoleDao menuRoleDao;

    @Autowired
    HrRoleDao hrRoleDao;

    public RespPageEntity getAllRoles(Integer page, Integer size, String roleName) {
        RespPageEntity entity = new RespPageEntity();
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<Role> allRoles = roleDao.getAllRoles(page, size, roleName);
        long total = roleDao.getTotal(roleName);
        entity.setData(allRoles);
        entity.setTotal(total);
        return entity;
    }

    public void addRole(Role role) {
        roleDao.insert(role);
    }

    public void modifyRole(Role role) {
        roleDao.updateByPrimaryKey(role);
    }

    public void deleteRole(Integer id) {
        roleDao.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public void menuAuthorization(Integer rid, List<Integer> menu_ids) {
        menuRoleDao.deleteByRoleId(rid);
        if (menu_ids.size() == 0) {
            return;
        }
        List<MenuRole> list = new ArrayList<>();
        for (int i = 0; i < menu_ids.size(); i++) {
            MenuRole menuRole = new MenuRole();
            menuRole.setRid(rid);
            menuRole.setMid(menu_ids.get(i));
            list.add(menuRole);
        }
        menuRoleDao.insertMultiline(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void userAuthorization(Integer user_id, List<Integer> role_ids) {
        hrRoleDao.deleteByUserId(user_id);
        if (role_ids.size() == 0) {
            return;
        }
        List<HrRole> list = new ArrayList<>();
        for (int i = 0; i < role_ids.size(); i++) {
            HrRole hrRole = new HrRole();
            hrRole.setHrid(user_id);
            hrRole.setRid(role_ids.get(i));
            list.add(hrRole);
        }
        hrRoleDao.insertMultiline(list);
    }

    public Map<String, Object> getRoleTree(Integer user_id) {
        Map<String, Object> result = new HashMap<>();
        List<Role> roles = roleDao.selectAllRoles();
        List<Role> ownerRoles = roleDao.selectiveByUserId(user_id);
        List<Tree> formData = new ArrayList<Tree>();
        for (Role role : roles) {
            if (!containsRole(ownerRoles, role)) {
                Tree tree = new Tree();
                tree.setId(role.getId());
                tree.setLabel(role.getNameZh());
                formData.add(tree);
            }
        }
        List<Tree> toData = new ArrayList<Tree>();
        for (Role role : ownerRoles) {
            Tree tree = new Tree();
            tree.setId(role.getId());
            tree.setLabel(role.getNameZh());
            toData.add(tree);
        }
        result.put("fromData", formData);
        result.put("toData", toData);
        return result;
    }

    public static boolean containsRole(List<Role> owner, Role target) {
        for (Role role : owner) {
            if (role.getId().equals(target.getId())) {
                return true;
            }
        }
        return false;
    }
}
